#include <windows.h>
#include <stdio.h>
#include <psapi.h>

int ID[1];
HANDLE work;

void PrintMemoryInfo( DWORD processID )
{
    HANDLE hProcess;
    PROCESS_MEMORY_COUNTERS pmc;

    // Print the process identifier.

    printf( "\nProcess ID: %u\n", processID );

    // Print information about the memory usage of the process.

    hProcess = OpenProcess(  PROCESS_QUERY_INFORMATION |
                                    PROCESS_VM_READ,
                                    FALSE, processID );
    if (NULL == hProcess)
        return;

    if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )
    {
        printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
        printf( "\tPeakWorkingSetSize: 0x%08X\n", 
                  pmc.PeakWorkingSetSize );
        printf( "\tWorkingSetSize: %d\n", pmc.WorkingSetSize/1024 );
        printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n", 
                  pmc.QuotaPeakPagedPoolUsage );
        printf( "\tQuotaPagedPoolUsage: 0x%08X\n", 
                  pmc.QuotaPagedPoolUsage );
        printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n", 
                  pmc.QuotaPeakNonPagedPoolUsage );
        printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n", 
                  pmc.QuotaNonPagedPoolUsage );
        printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage ); 
        printf( "\tPeakPagefileUsage: 0x%08X\n", 
                  pmc.PeakPagefileUsage );
    }

    CloseHandle( hProcess );
}
void runWB()
{
    STARTUPINFO procSt;
    PROCESS_INFORMATION procInfo;
    STARTUPINFO procSt2;
    PROCESS_INFORMATION procInfo2;

    memset(&procSt, 0, sizeof(procSt));
    memset(&procInfo, 0, sizeof(procInfo));
    memset(&procSt2, 0, sizeof(procSt2));
    memset(&procInfo2, 0, sizeof(procInfo2));

      procSt.dwFlags = STARTF_USESHOWWINDOW;
      procSt.wShowWindow = SW_HIDE;
      procSt2.dwFlags = STARTF_USESHOWWINDOW;
      procSt2.wShowWindow = SW_HIDE;

    procSt.cb = sizeof(procSt);
    procSt2.cb = sizeof(procSt2);

    //Path points to the workbench executable.
    printf("Starting workbench program\n");
    if (!CreateProcess("C:\\Mosaic\\NovaBench\\nb3.exe", NULL, NULL, NULL, false, CREATE_NO_WINDOW, NULL, NULL, &procSt, &procInfo))
    {
        printf("Couldn't run it\n");
    }
    else
    {
        Sleep(5000);
        CreateProcess("C:\\Mosaic\\startworkbench.exe", NULL, NULL, NULL, false, CREATE_NO_WINDOW, NULL, NULL, &procSt2, &procInfo2);
        printf("Workbench running. Process ID = %d\n", procInfo.dwProcessId);
    }

    ID[0] = procInfo.dwProcessId;
    ID[1] = procInfo2.dwProcessId;
    work = procInfo.hProcess;
}

int main( void )
{
    // Get the list of process identifiers.

    DWORD aProcesses[1024], cbNeeded, cProcesses;

    if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
    {
        return 1;
    }

    // Calculate how many process identifiers were returned.

    cProcesses = cbNeeded / sizeof(DWORD);

    //Run workbench.
    runWB();

    for (int j = 0; j < 5; j++)
    {
        PrintMemoryInfo(ID[0]);
        Sleep(3000);
    }

    TerminateProcess(work, 0);
    printf("Workbench program exited\n");

    return 0;
}